//
//  HLogin.m
//  Project
//
//  Created by 黄振 on 16/8/28.
//  Copyright © 2016年 黄振. All rights reserved.
//

#import "HLogin.h"
#import "HAPIManager.h"
#import <SSKeychain.h>
#import "HCommon.h"
#import <TMCache.h>
#define dispatch_once _dispatch_once

static NSString *PhoneNumber = @"phoneNumber";
static NSString *Password = @"password";
static NSString *ThothLoginService = @"thothLoginService";

//#define PhoneNumber PhoneNumber
//#define PhonePassword Password


@interface HLogin()

@end

@implementation HLogin
@synthesize password = _password;
@synthesize mobileNumber = _mobileNumber;
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static HLogin *instance;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}


-(void)setPassword:(NSString *)password
{
    _password = password;
    [[TMCache sharedCache]setObject:password forKey:Password];
}

- (NSString *)password
{
    if (_password.length == 0) {
        _password = [[TMCache sharedCache]objectForKey:Password];
    }
    return _password;
}

- (NSString *)mobileNumber
{
    if (_mobileNumber.length == 0) {
        _mobileNumber = [[TMCache sharedCache]objectForKey:PhoneNumber];
    }
    return _mobileNumber;
}

- (void)setMobileNumber:(NSString *)mobileNumber
{
    _mobileNumber = mobileNumber;
    [[TMCache sharedCache]setObject:mobileNumber forKey:PhoneNumber];
}
-(BOOL)isAutoLogin
{
    return self.mobileNumber.length>0;
    
}

- (void)login
{
    if (self.isAutoLogin) {
        @weakify(self);
        [HCommon popupMessage:@"登录中..."];
        [[[self loginWithMobileNumber:self.mobileNumber password:self.password] doError:^(NSError *error) {
            @strongify(self);
            self.password = @"";
            [HCommon popupErrorMessage:error.userInfo[@"message"]];
        }]subscribeNext:^(id x) {
            [HCommon popupSuccessMessage:@"登录成功"];
        }];
    }
}

//登录
- (RACSignal *)loginWithMobileNumber:(NSString *)mobileNumber
                            password:(NSString *)password {
    
    @weakify(self);
    id loginFailure = ^(NSError *error) {
        @strongify(self);
        self.loginStatus = LoginStatus_Failure;
    };
    
    id loginSuccess = ^(id x) {
        @strongify(self);
        self.loginStatus = LoginStatus_Success;
        self.mobileNumber = mobileNumber?:@"";
        self.password = password?:@"";
    };
    
    return [[[[HAPIManager sharedInstance]userLoginWithAccount:mobileNumber password:password]doError:loginFailure]doNext:loginSuccess];
    
}
//退出登录
- (RACSignal *)logout {
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [[TMCache sharedCache]removeObjectForKey:PhoneNumber];
        [[TMCache sharedCache]removeObjectForKey:Password];
        self.mobileNumber = @"";
        self.password = @"";
        self.loginStatus = LoginStatus_Logout;
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        return nil;
    }];
}
@end
